using System.Collections.Generic;
using System.Collections.Specialized;

namespace HooIzDat.Web.Models
{
    public partial class Character
    {
        public string Image
        {
            get { return "~/Content/CharacterImages/" + CharacterID.ToString() + ".jpg"; }
        }

        public virtual int CharacterID
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }

        public virtual ICollection<Game> Games
        {
            get
            {
                if (_games == null)
                {
                    var newCollection = new FixupCollection<Game>();
                    newCollection.CollectionChanged += FixupGames;
                    _games = newCollection;
                }
                return _games;
            }
            set
            {
                if (!ReferenceEquals(_games, value))
                {
                    var previousValue = _games as FixupCollection<Game>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGames;
                    }
                    _games = value;
                    var newValue = value as FixupCollection<Game>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGames;
                    }
                }
            }
        }
        private ICollection<Game> _games;
    
        public virtual ICollection<Game> Games1
        {
            get
            {
                if (_games1 == null)
                {
                    var newCollection = new FixupCollection<Game>();
                    newCollection.CollectionChanged += FixupGames1;
                    _games1 = newCollection;
                }
                return _games1;
            }
            set
            {
                if (!ReferenceEquals(_games1, value))
                {
                    var previousValue = _games1 as FixupCollection<Game>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGames1;
                    }
                    _games1 = value;
                    var newValue = value as FixupCollection<Game>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGames1;
                    }
                }
            }
        }
        private ICollection<Game> _games1;
    
        public virtual ICollection<GameState> GameStates
        {
            get
            {
                if (_gameStates == null)
                {
                    var newCollection = new FixupCollection<GameState>();
                    newCollection.CollectionChanged += FixupGameStates;
                    _gameStates = newCollection;
                }
                return _gameStates;
            }
            set
            {
                if (!ReferenceEquals(_gameStates, value))
                {
                    var previousValue = _gameStates as FixupCollection<GameState>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGameStates;
                    }
                    _gameStates = value;
                    var newValue = value as FixupCollection<GameState>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGameStates;
                    }
                }
            }
        }
        private ICollection<GameState> _gameStates;
    
        public virtual ICollection<GameTile> GameTiles
        {
            get
            {
                if (_gameTiles == null)
                {
                    var newCollection = new FixupCollection<GameTile>();
                    newCollection.CollectionChanged += FixupGameTiles;
                    _gameTiles = newCollection;
                }
                return _gameTiles;
            }
            set
            {
                if (!ReferenceEquals(_gameTiles, value))
                {
                    var previousValue = _gameTiles as FixupCollection<GameTile>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupGameTiles;
                    }
                    _gameTiles = value;
                    var newValue = value as FixupCollection<GameTile>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupGameTiles;
                    }
                }
            }
        }
        private ICollection<GameTile> _gameTiles;
    
        public virtual ICollection<CharacterTrait> CharacterTraits
        {
            get
            {
                if (_characterTraits == null)
                {
                    var newCollection = new FixupCollection<CharacterTrait>();
                    newCollection.CollectionChanged += FixupCharacterTraits;
                    _characterTraits = newCollection;
                }
                return _characterTraits;
            }
            set
            {
                if (!ReferenceEquals(_characterTraits, value))
                {
                    var previousValue = _characterTraits as FixupCollection<CharacterTrait>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupCharacterTraits;
                    }
                    _characterTraits = value;
                    var newValue = value as FixupCollection<CharacterTrait>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupCharacterTraits;
                    }
                }
            }
        }
        private ICollection<CharacterTrait> _characterTraits;

        private void FixupGames(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Game item in e.NewItems)
                {
                    item.Player1Character = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Game item in e.OldItems)
                {
                    if (ReferenceEquals(item.Player1Character, this))
                    {
                        item.Player1Character = null;
                    }
                }
            }
        }
    
        private void FixupGames1(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Game item in e.NewItems)
                {
                    item.Player2Character = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Game item in e.OldItems)
                {
                    if (ReferenceEquals(item.Player2Character, this))
                    {
                        item.Player2Character = null;
                    }
                }
            }
        }
    
        private void FixupGameStates(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (GameState item in e.NewItems)
                {
                    item.GuessedCharacter = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (GameState item in e.OldItems)
                {
                    if (ReferenceEquals(item.GuessedCharacter, this))
                    {
                        item.GuessedCharacter = null;
                    }
                }
            }
        }
    
        private void FixupGameTiles(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (GameTile item in e.NewItems)
                {
                    item.Character = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (GameTile item in e.OldItems)
                {
                    if (ReferenceEquals(item.Character, this))
                    {
                        item.Character = null;
                    }
                }
            }
        }
    
        private void FixupCharacterTraits(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (CharacterTrait item in e.NewItems)
                {
                    item.Character = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (CharacterTrait item in e.OldItems)
                {
                    if (ReferenceEquals(item.Character, this))
                    {
                        item.Character = null;
                    }
                }
            }
        }
    }
}
